#include "VersionInfo.h"
#include "Log.h"
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


char* build_month(char date[50]){
	if(date[0] == 'J' && date[1] == 'a' && date[2] == 'n'){
		return "01";
	}else if(date[0] == 'F'){
		return "02";
	}else if(date[0] == 'M' &&date[1] == 'a' && date[2] == 'r'){
		return "03";
	}else if(date[0] == 'A' && date[1] == 'p'){
		return "04";
	}else if(date[0] == 'M' && date[1] == 'a' && date[2] == 'y'){
		return "05";
	}else if(date[0] == 'J' && date[1] == 'u' && date[2] == 'n'){
		return "06";
	}else if(date[0] == 'J' && date[1] == 'u' && date[2] == 'l'){
		return "07";
	}else if(date[0] == 'A' && date[1] == 'u'){
		return "08";
	}else if(date[0] == 'S'){
		return "09";
	}else if(date[0] == 'O'){
		return "10";
	}else if(date[0] == 'N'){
		return "11";
	}else if(date[0] == 'D'){
		return "12";
	}
	return "01";
}

void build_day(char date[50],char*day){

	char  day_char0 = ((date[4] >= '0') ? (date[4]) : '0');
	char  day_char1 = date[5];

	day[0] = day_char0;
	day[1] = day_char1;
	day[2] = '\0';
}

void build_hour(char time[50],char*hour){

	char  hour_char0 = ((time[0] >= '0') ? (time[0]) : '0');
	char  hour_char1 = time[1];

	hour[0] = hour_char0;
	hour[1] = hour_char1;
	hour[2] = '\0';
}

void build_minute(char time[50],char*min){

	char  min_char0 = ((time[3] >= '0') ? (time[3]) : '0');
	char  min_char1 = time[4];

	min[0] = min_char0;
	min[1] = min_char1;
	min[2] = '\0';
}

int version_info(char * version, int versionLen){

	int					n = 0;
	int					len = 0;

	char date[50];
	char time[50];
	snprintf( date, sizeof(date), "%s",  __DATE__ );
	snprintf( time, sizeof(time), "%s",  __TIME__ );
//	LOGD( "%s date: %s \n", __func__, date);
//	LOGD( "%s time: %s \n", __func__, time);

	char year [20];
	memset(year,0,20);
	memcpy(year,date+7,4);
	n = sprintf( &version[ len ], "XQCarPlay_%s",year );
	len += ( n );

	char * month = build_month(date);
	n = sprintf( &version[ len ], "%s",month );
	len += ( n );

	char * day = (char *) malloc(3);
	build_day(date,day);
	n = sprintf( &version[ len ], "%s",day );
	len += ( n );

	char * hour = (char *) malloc(3);
	build_hour(time,hour);
	n = sprintf( &version[ len ], "%s",hour );
	len += ( n );

	char * min = (char *) malloc(3);
	build_minute(time,min);
	n = sprintf( &version[ len ], "%s",min );
	len += ( n );


	LOGD( "%s compileVersion: %s \n", __func__, version);
	free(day);
	free(hour);
	free(min);

	return 0;
}
